#include <bits/stdc++.h>
using namespace std;
int ma[1003][1003];
int v[1003][1003]; //存储每个点访问的最少步数
//四个方向
int di[] = {0, 0, -1, 1};
int dj[] = {1, -1, 0, 0};
using pp = pair<int, int>;

queue<pp> Q;
int n, m;
//判断是否越界
bool check(int i, int j) { return i <= n && i >= 1 && j <= m && j >= 1; }
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> ma[i][j];
    }
  }

  //宽搜
  Q.push(make_pair(1, 1));
  v[1][1] = 1;
  while (!Q.empty()) {
    pp f = Q.front();
    Q.pop();
    for (int d = 0; d < 4; d++) {
      int i = f.first;
      int j = f.second;
      int t = ma[i][j];
      int ni = i + di[d] * t;
      int nj = j + dj[d] * t;
      if (check(ni, nj) && !v[ni][nj]) {
        Q.push(make_pair(ni, nj));
        v[ni][nj] = v[i][j] + 1;
      }
    }
  }
  if (!v[n][m])
    cout << "No Solution";
  else
    cout << v[n][m] - 1;
  return 0;
}